package com.enjoy.seata.util;

import com.alibaba.druid.pool.DruidDataSource;
import io.seata.rm.datasource.DataSourceProxy;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 获取不同的数据源
 */
public class DataSourceUtil {

    private static final Map<String, DataSource> dataSourceMap = new ConcurrentHashMap<>();


    public static Connection getConnection(String dataSourceCode) {
        DataSource dataSource = dataSourceMap.get(dataSourceCode);
        try {
            if (dataSource != null) {
                return dataSource.getConnection();
            } else {
                DruidDataSource druidDataSource = new DruidDataSource();
                String url = "jdbc." + dataSourceCode + ".url";
                String username = "jdbc." + dataSourceCode + ".username";
                String password = "jdbc." + dataSourceCode + ".password";
                String driver = "jdbc." + dataSourceCode + ".driver";

                druidDataSource.setDriverClassName(PropertiesUtil.getPropertyValue(driver));
                druidDataSource.setUrl(PropertiesUtil.getPropertyValue(url));
                druidDataSource.setUsername(PropertiesUtil.getPropertyValue(username));
                druidDataSource.setPassword(PropertiesUtil.getPropertyValue(password));

                DataSourceProxy dataSourceProxy = new DataSourceProxy(druidDataSource);
                dataSourceMap.put(dataSourceCode, dataSourceProxy);
                return dataSourceProxy.getConnection();
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }

    public static void executeUpdate(String code, String sql) {
        Connection connection = null;
        Statement statement = null;

        try {
            connection = getConnection(code);
            System.out.println("自动提交:"+connection.getAutoCommit());
            statement = connection.createStatement();
            statement.executeUpdate(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (connection != null) {
                    connection.close();
                }
                if (statement != null) {
                    statement.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }

    public static String getSingleResult(String code, String sql) {
        Connection connection = null;
        Statement statement = null;
        String result = null;
        try {
            connection = getConnection(code);
            statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery(sql);
            resultSet.next();
            result = resultSet.getString(1);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (connection != null) {
                    connection.close();
                }
                if (statement != null) {
                    statement.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

}
